<?php


namespace app\api\controller;


use think\facade\Db;

class AuthRule
{
    public function getAuthRule(){
        $param_data = request()->param();
        unset($param_data['page']);
        $data = Db::table('think_auth_rule')->where($param_data)->select()->toArray();
        if(count($data)>1){
            $datas = $this->getTree($data);
            $city = $datas;
            $data2 = $datas;
        }else{
            $city = $data;
            $datas = $this->getTree(Db::table('think_auth_rule')->select());
            $data2 = $datas;
        }
        $unshift = array('id'=>0,'f_id'=>0,'title'=>'顶级权限','level'=>-1);
        array_unshift($data2,$unshift);
        return json(['status'=>200,'data'=>$city,'data2'=>$data2,'total'=>count($city)]);
    }
    public function getCity2(){
        $city = $this->getTree2();
        return json(['status'=>200,'data'=>$city,'total'=>count($city)]);
    }
    //递归
    public function getTree($data,$f_id =0)
    {
        $list = [];
        foreach ($data as $value) {
            //第一次遍历，找到父节点为0的
            if ($value['f_id'] == $f_id) {
                //开始递归，查找父id为该节点id的节点
                if (!empty($this->getTree($data, $value['id']))) {
                    $value['children'] = $this->getTree($data, $value['id']);
                }
                $list[] = $value;
            }
        }
        return $list;
    }
    protected function getTree2($f_id=0,$level=0){
        $zhi =  Db::table('city')->select()->toArray();
        static $arr =array();
        foreach($zhi as $key=>$value){
            if($value['f_id']==$f_id){
                $value['level']=$level;
                $arr[] =$value;
                //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
                $this->getTree2($value['id'],$level+1);
            }
        }
        return $arr;
    }
    public function createAuthRule(){
        $data = request()->param();
        $data['level'] = Db::table("think_auth_rule")->where('id',$data['f_id'])->value('level')+1;
        $add = Db::table("think_auth_rule")->insert($data);
        if($add){
            return json(['status'=>200,'msg'=>'添加成功']);
        }else{
            return json(['status'=>500,'msg'=>'添加失败']);
        }
    }
    public function updateAuthRule(){
        $data = request()->param();
        if($data['id']==$data['f_id']){
            return json(['status'=>500,'msg'=>'自己不能当自己的上级！']);
        }
        $data['level'] = Db::table("think_auth_rule")->where('id',$data['f_id'])->value('level')+1;
        unset($data['children']);
        $update = Db::table("think_auth_rule")->where('id',$data['id'])->update($data);
        if($update){
            return json(['status'=>200,'msg'=>'修改成功']);
        }else{
            return json(['status'=>500,'msg'=>'修改失败']);
        }
    }
    public function deleteAuthRule(){
        $data = request()->param();
        $sonid =  $this->getSonId($data['id']);
        if(empty($sonid)){//没有子id，删除自己
            $del = Db::table("think_auth_rule")->where('id',$data['id'])->delete();
        }else{
            array_unshift($sonid,$data['id']);
            $del = Db::table("think_auth_rule")->whereIn('id',$sonid)->delete();
        }
        if($del){
            return json(['status'=>200,'msg'=>'删除成功']);
        }else{
            return json(['status'=>500,'msg'=>'删除失败']);
        }
    }
    protected function getSonId($id){
        $data =  Db::table('think_auth_rule')->select()->toArray();
        $arr = [];
        foreach ($data as $val){
            if($val['f_id']==$id){
                $arr[] = $val['id'];
            }
        }
        return $arr;
    }
}